package com.ktc.base.service;

import com.ktc.base.dao.LabelDao;
import com.ktc.base.entity.Label;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
@Service
public class LabelService {

    @Autowired
    private LabelDao labelDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 根据id查询标签
     *
     * @param lableid
     * @return
     */
    public Label findById(String lableid) {
        return labelDao.findById(lableid).get();
    }

    /**
     * 新增
     *
     * @param label
     */
    public void save(Label label) {

        label.setId(idWorker.nextId() + "");
        labelDao.save(label);

    }

    public void delete(String labelid) {
        labelDao.deleteById(labelid);
    }

    public void update(Label label) {
        labelDao.save(label);
    }

    public List<Label> findAll() {
        return labelDao.findAll();
    }

    /**
     * 根据前端传递的条件,生成Spec条件对象
     *
     * @param searchMap
     * @return
     */
    private Specification<Label> createSpec(Map searchMap) {

        Specification spec = new Specification() {
            /**
             *
             * @param root:根对象
             * @param criteriaQuery: 离线查询对象
             * @param criteriaBuilder: 条件构建对象
             * @return
             */
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {


                List<Predicate> predicateList = new ArrayList<>();


                if (searchMap.get("id") != null && !searchMap.get("id").equals("")) {


                    // 封装一个条件对象Predicate
                    Path idPath = root.get("id");

                    Expression idExp = idPath.as(String.class);

                    Predicate predicate = criteriaBuilder.equal(idExp, searchMap.get("id"));

                    predicateList.add(predicate);

                }


                if (searchMap.get("labelname") != null && !searchMap.get("labelname").equals("")) {
                    // 封装一个条件对象Predicate
                    predicateList.add(criteriaBuilder.like(root.get("labelname").as(String.class), searchMap.get("labelname").toString()));
                }

                Predicate[] predicates = new Predicate[predicateList.size()];

                return criteriaBuilder.and(predicateList.toArray(predicates));
            }
        };

        return spec;
    }

    public List<Label> search(Map searchMap) {


        Specification<Label> spec = createSpec(searchMap);
        List<Label> labelList = labelDao.findAll(spec);
        return labelList;
    }

    /**
     * 条件分页＋搜索
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public Page<Label> findPage(Map searchMap, Integer page, Integer size) {

        Page<Label> pageData = labelDao.findAll(createSpec(searchMap), PageRequest.of(page - 1, size));

        return pageData;
    }
}
